package com.itsu.application.service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.itsu.application.dao.UserDao;
import com.itsu.application.entity.User;

public class LoginService {

	private UserDao userDao = new UserDao();

	public User login(User user) throws Exception {
		List<User> users = userDao.selectUsers(user);
		if (users == null || users.isEmpty()) {
			throw new Exception("用戶名或密碼錯誤");
		}
		return users.get(0);
	}

	public List<User> getFriends(User user) throws Exception {
		return userDao.selectUsersBySql("select * from tb_user where user_id != ?", user.getUserId());
	}

	public List<String> getFriendsIdName(User user) throws Exception {
		List<User> friends = getFriends(user);
		return friends.stream().map(x -> {
			String id = x.getUserId();
			String name = x.getUserName();
			return name + "(" + id + ")";
		}).collect(Collectors.toList());
	}
}
